package com.zwb.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwb.boot.entity.AddressBook;
import com.zwb.boot.mapper.AddressBookMapper;
import com.zwb.boot.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 头发又黑又长
 * @Date 2022/7/25 19:14
 * @email zwb15083976291@163.com
 */
@Slf4j
@Service
@Transactional
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook>
        implements AddressBookService {

    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;


    // 修改默认地址
    @Override
    public void setDefault(Long id) {
        // 修改原来的默认地址
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getIsDefault, 1)
                .set(AddressBook::getIsDefault, 0);
        addressBookMapper.update(null, updateWrapper);
        // 修改 id 为默认地址
        LambdaUpdateWrapper<AddressBook> updateDefaultWrapper = new LambdaUpdateWrapper<>();
        updateDefaultWrapper.eq(AddressBook::getId, id)
                .set(AddressBook::getIsDefault, 1);
        addressBookMapper.update(null, updateDefaultWrapper);
    }
}
